/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ddd.service;

import com.ddd.context.Parameters;
import com.ddd.entity.Idtable;
import com.ddd.util.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class IDService extends BaseService {

    private Parameters para;
    private LogService logService;

    public Idtable load(String tablename) {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();

        Idtable idtable = (Idtable) session.load(Idtable.class, tablename);
        //Hibernate.initialize(journal);

        tx.commit();
        session.close();

        return idtable;
    }

    public String getNextID(String tableName) {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();

        Idtable idtable = (Idtable) session.load(Idtable.class, tableName);
        //Hibernate.initialize(idtable);
        int id = idtable.getCurrId().intValue();
        idtable.setCurrId(new Integer(id + 1));
        session.update(idtable);


        tx.commit();
        session.close();

        return String.valueOf(idtable.getCurrId());
    }

    public void save(Idtable idtable) {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();
        session.save(idtable);
        tx.commit();
        session.close();
    }
}
